<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.js"></script>
		<script src="/js/plugins/jquery-form/jquery.form.js"></script>
		<script src="/js/common.js"></script>
		<script type="text/javascript">
			$(function () {
			    var type = 0; //表示当前操作的图片 0:头像 1:背景

                var user = getUser();
			    //渲染界面
			    $(".info").renderValues(user);
			    //渲染背景
				$(".myBg").css("background-image","url('"+user.coverImgUrl+"')");

				//点击保存按钮以PUT方式提交表单数据
				$("#saveBtn").click(function () {
				    //重新配置参数
					$("#infoForm").ajaxSubmit({
						url: "/users/"+user.id,
						success:　function (data) {
                            //重新缓存当前对象
                            data.obj.email = user.email; //填充email
							setUser(data.obj);
							location.href = "/mine/profiles.html";
                        }
					});
                });

				//点击图片弹出文件上传控件
				$("#headImg,#coverBtn").click(function () {
				    //设置当前操作的类型
					type = $(this).data("type");
				    //使用编程的方式来点击文件上传控件
					$(":file").click();
                });

				//给隐藏的文件上传控件绑定值改变事件
                $(":file").change(function () {
                    if (this.value) {
                        //上传图片
						$("#imgForm").ajaxSubmit(function (data) {
                            //回填图片URL
							if (type == 0) {
							    //回显头像
                                $("#headImg").attr("src", data.url);
                                $("#infoForm input[name='headImgUrl']").val(data.url);
							} else {
							    //回显背景
                                $(".myBg").css("background-image","url('"+data.url+"')");
                                $("#infoForm input[name='coverImgUrl']").val(data.url);
							}

                        });
                    }
                });
            });
		</script>
	</head>

	<body>
		<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
			<input type="file" name="pic" style="display: none;" accept="image/*"/>
		</form>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:history.back();">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="javascript:" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg" data-type="0" render-src="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" >
					<i class="fa fa-pencil-square fa-lg" aria-hidden="true"></i>
				</div>
				<button  class="btn btn-outline-light ibtn" id="coverBtn" data-type="1">设置背景</button>
			</div>

			<form id="infoForm" method="post">
				<input type="hidden" name="_method" value="PUT">
				<!--隐藏当前用户图片的地址-->
				<input type="hidden" name="headImgUrl" render-value="headImgUrl">
				<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
				<label>昵称</label>
				<input type="text" name="nickName" render-value="nickName" class="form-control" />
				<label>性别</label>
				<select class="form-control" name="gender" render-value="gender">
					<option value="0">保密</option>
					<option value="1">男</option>
					<option value="2">女</option>
				</select>
				<label>地区</label>
				<input type="text" name="place" render-value="place" class="form-control">
				<label>签名</label>
				<textarea name="sign" class="form-control" rows="5" render-html="sign"></textarea>
			</form>
		</div>

	</body>
</html>
